﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using XWG.JWBST.ProcessInterfaces;
using System.IO;
using System.Web;
using XWG.Framework.AuthUtility;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;

namespace XWG.JWBST.OnlineReportProcess
{
   /// <summary>
    /// 在线报案
   /// </summary>
   public class Process : IProcess
    {
       /// <summary>
       /// 在线报案
       /// </summary>
       /// <param name="reader"></param>
       /// <param name="context"></param>
       /// <returns></returns>
       public string ProcessData(StreamReader reader, HttpContext context)
       {
           // 读取提交数据
           string receivemes = reader.ReadToEnd();

           // UrlDecode解码
           receivemes = HttpUtility.UrlDecode(receivemes);

           // 去除content=无用数据 提交的时候不要此key

           //receivemes = receivemes.Replace("content=", "");

           // 反序列化json对象
            JObject oka = (JObject)JsonConvert.DeserializeObject(receivemes);
            XWG.JWBST.JWBSTEntity.TbBstOnlinereport report = new JWBSTEntity.TbBstOnlinereport();
            report.Reportid = Guid.NewGuid().ToString();
            report.RPhone = oka["phone"].ToString().Replace("\"", string.Empty);
            report.RContent = oka["content"].ToString().Replace("\"", string.Empty);
            report.RGpsX = oka["GPS_JD"].ToString().Replace("\"", string.Empty);
            report.RGpsY = oka["GPS_WD"].ToString().Replace("\"", string.Empty);
            report.Movesign = "0";
            report.RTsign = "0";
            //report.RImage = null;
            report.RDate = DateTime.Now;
            ResultModel re = new ResultModel();
            re = XWG.JWBST.JWBSTBLL.TbBstOnlinereportBLL.Insert(report,"",false);
            if (re.ISSucceed)
            {
                //记录请求信息
                JWBSTEntity.TbBstVisits visit = new JWBSTEntity.TbBstVisits();
                visit.Id = Guid.NewGuid().ToString();
                visit.Phone = report.RPhone;
                visit.Time = DateTime.Now;
                visit.Modulename = "OnlineReport";
                visit.Optionname = "在线报案";
                visit.Other = oka.ToString();
                JWBSTBLL.TbBstVisitsBLL.Insert(visit, "", false);

                JObject rejson = new JObject();
                rejson.Add("uid", re.FiledKey);
                string json = JsonConvert.SerializeObject(rejson);

                return json; 
            }
            else
            {
                return "操作失败！";
            }

           
       }

       //  Stream inStream = context.Request.InputStream;
       //     report.RImage = SetStreamToBytes(inStream);

       //public byte[] SetStreamToBytes(Stream stream)
       //{
       //    try
       //    {
       //        byte[] bytes = new byte[stream.Length];
       //        stream.Read(bytes, 0, bytes.Length);
       //        // 设置当前流的位置为流的开始 
       //        stream.Seek(0, SeekOrigin.Begin);
       //        return bytes;
       //    }
       //    catch (Exception ex)
       //    {
       //        throw new Exception(ex.Message);
       //    }
       //}
    }
}
